import store from './store'
import router from './router'
// 加载进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const whiteList = ['/login', '/404']
// 导航守卫
router.beforeEach(async (to, from, next) => {
  const token = store.getters.token

  NProgress.start()
  if (token) {
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      const userInfo = store.state.user.userInfo
      if (Object.keys(userInfo).length <= 0) {
        await store.dispatch('user/changeUserInfo')
        // console.log(res)
      }
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach((to, from) => {
  NProgress.done()
})
